class InstancesController < ApplicationController
  layout nil
  
  def index
    respond_to do |format|
      format.html {
        render
      }
      
      format.json {
        instances = []
        Ec2Factory.create(User.find(session[:current_user_id]), session[:url]).describe_instances.each_with_index{|rawInstance, i|
          p rawInstance
          
          instances << {:id => i + 1, :cell => [
            rawInstance[:aws_instance_id], 
            rawInstance[:aws_image_id],
            rawInstance[:dns_name],
            rawInstance[:private_dns_name],
            rawInstance[:aws_state],
            rawInstance[:aws_instance_type],
            rawInstance[:aws_launch_time],
            rawInstance[:aws_availability_zone],
          ]}
        }
        
        render :json => {
          :page => params[:page],
          :total => instances.length / params[:rows].to_i + 1,
          :records => instances.length,
          :rows => instances 
        }.to_json
      }
    end
  end

end
